package net.schmurgon.gwt.ui;

import com.google.gwt.app.place.AbstractActivity;
import com.google.gwt.app.place.PlaceController;
import com.google.gwt.app.place.ProxyPlace;
import com.google.gwt.app.place.RecordDetailsView;
import com.google.gwt.app.place.ProxyPlace.Operation;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.RequestObject;
import com.google.gwt.valuestore.shared.SyncResult;
import com.google.gwt.valuestore.shared.Value;
import net.schmurgon.gwt.request.RunnerRecord;
import net.schmurgon.gwt.shared.Gender;
import net.schmurgon.gwt.request.ApplicationRequestFactory;
import net.schmurgon.gwt.scaffold.generated.RunnerDetailsView;

import java.util.Set;

/**
 * An {@link com.google.gwt.app.place.Activity Activity} that requests and
 * displays detailed information on a given Runner.
 */
public class RunnerDetailsActivity extends AbstractActivity implements RecordDetailsView.Delegate {
	private static RecordDetailsView<RunnerRecord> defaultView;
	
	private static RecordDetailsView<RunnerRecord> getDefaultView() {
		if (defaultView == null) {
			defaultView = new RunnerDetailsView();
		}
		return defaultView;
	}

	private final ApplicationRequestFactory requests;
	private final PlaceController placeController;
	private final RecordDetailsView<RunnerRecord> view;
	private RunnerRecord record;
	private Display display;

	/**
	 * Creates an activity that uses the default singleton view instance.
	 */
	public RunnerDetailsActivity(RunnerRecord proxy, ApplicationRequestFactory requests, PlaceController placeController) {
		this(proxy, requests, placeController, getDefaultView());
	}

	/**
   * Creates an activity that uses its own view instance.
 	 */
	public RunnerDetailsActivity(RunnerRecord proxy, ApplicationRequestFactory requests, PlaceController placeController, RecordDetailsView<RunnerRecord> view) {
		this.placeController = placeController;
		this.record = proxy;
		this.requests = requests;
		view.setDelegate(this);
		this.view = view;
	}

	public void deleteClicked() {
		if (!view.confirm("Really delete this record? You cannot undo this change.")) {
			return;
		}
   		
		RequestObject<Void> deleteRequest = requests.runnerRequest().remove(view.getValue());
		deleteRequest.fire(new Receiver<Void>() {
			public void onSuccess(Void ignore, Set<SyncResult> response) {
				if (display == null) {
					// This activity is dead
					return;
				}

				display.showActivityWidget(null);
			}
		});
	}

	public void editClicked() {
		placeController.goTo(new ProxyPlace(view.getValue(), Operation.EDIT));
	}

	public void onCancel() {
		onStop();
	}

	public void onStop() {
		display = null;
	}

	public void start(Display displayIn, EventBus eventBus) {
    	this.display = displayIn;
    	Receiver<RunnerRecord> callback = new Receiver<RunnerRecord>() {
			public void onSuccess(RunnerRecord record, Set<SyncResult> syncResults) {
        		if (display == null) {
          			return;
       			}
				view.setValue(record);
				display.showActivityWidget(view);
			}
		};

		requests.runnerRequest().findRunner(Value.of(record.getId())).with().fire(callback);
	}
}
